package com.example.anni.zhuzhoujia.xiaowan;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.anni.zhuzhoujia.R;
import com.lhh.ptrrv.library.PullToRefreshRecyclerView;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Anni on 2016/7/8.
 */
public class MyCommentActivity extends AppCompatActivity {
    private PullToRefreshRecyclerView mPtrrv;
    private List<Map<String, String>> mData;
    private MyCommentAdapter mMyCommentAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_comment_activity);
        initToolbar();
        initData();
        initView();
    }

    private void initToolbar() {
        TextView tvNameTitle;
        tvNameTitle = (TextView) findViewById(R.id.tv_name_title);
        tvNameTitle.setText("我的评论");
    }

    private void initView() {
        mPtrrv = (PullToRefreshRecyclerView) findViewById(R.id.ptrrv_my_comment);
        //线性管理器
        LinearLayoutManager layoutManager =
                new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mPtrrv.setLayoutManager(layoutManager);
        //设置下划线
        mPtrrv.getRecyclerView().addItemDecoration(
                new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));

    }

    private void initData() {
        mData = new ArrayList<>();
        RequestParams params =
                new RequestParams("http://tr.zzapi.gson.cn/api/user/comments");
        params.addQueryStringParameter("page", "1");
        params.addQueryStringParameter("rows", "10");
        x.http().get(params, new CallBackAdapter<String>() {
            @Override
            public void onSuccess(String s) {
                JSONObject obj = JSON.parseObject(s);
                Boolean success = obj.getBoolean("success");
                if (success) {
                    JSONArray arr = obj.getJSONArray("data");
                    for (int i = 0; i < arr.size(); i++) {
                        Map<String, String> map = new HashMap<>();
                        map.put("content", arr.getJSONObject(i).getString("content"));
                        map.put("id", arr.getJSONObject(i).getString("id"));
                        map.put("newsId", arr.getJSONObject(i).getString("newsId"));
                        map.put("dateline", arr.getJSONObject(i).getString("dateline"));
                        map.put("newsTitle", arr.getJSONObject(i).getString("newsTitle"));
                        SharedPreferences sp =
                                PreferenceManager.getDefaultSharedPreferences(MyCommentActivity.this);
                        String avatar = sp.getString("avatar", "");
                        String nickName = sp.getString("nickName", "");
                        map.put("avatar", avatar);
                        map.put("nickName", nickName);
                        mData.add(map);
                    }
                    mMyCommentAdapter = new MyCommentAdapter(MyCommentActivity.this, mData);
                    mPtrrv.setAdapter(mMyCommentAdapter);

                }
            }
        });

    }

}
